package com.example.admin.mybledemo.ota;

import java.util.List;

import cn.com.heaton.blelibrary.ble.BleLog;
import cn.com.heaton.blelibrary.ble.model.BleDevice;

/**
 * OTA设备检查工具类
 * 与Flutter端的isOtaDevice逻辑保持一致
 */
public class OtaDeviceChecker {
    
    private static final String TAG = "OtaDeviceChecker";
    
    // OTA服务UUID常量（与OtaProtocol保持一致）
    private static final String APP_OTA_SERVICE_UID = "5265616C-6D70-6761-2D49-6E73696465AA";
    private static final String APP_OTA_SERVICE_UID_V2 = "5265616C-6D80-6761-2D49-6E73696465AA";
    private static final String BOOT_OTA_SERVICE_UID = "5265616C-6D65-6761-2D49-6E73696465AA";
    
    /**
     * 检查设备是否支持OTA升级（与Flutter端isOtaDevice逻辑一致）
     * 
     * @param device BLE设备
     * @param serviceUUIDs 设备服务UUID列表
     * @return 检查结果，包含是否支持OTA和设备模式信息
     */
    public static OtaDeviceCheckResult checkOtaDevice(BleDevice device, List<String> serviceUUIDs) {
        BleLog.d(TAG, "开始检查设备是否支持OTA: " + device.getBleName() + " (" + device.getBleAddress() + ")");
        
        if (serviceUUIDs == null || serviceUUIDs.isEmpty()) {
            BleLog.d(TAG, "设备服务列表为空");
            return new OtaDeviceCheckResult(false, OtaDeviceMode.UNKNOWN, "设备服务列表为空");
        }
        
        for (String serviceUUID : serviceUUIDs) {
            String uuid = serviceUUID.toUpperCase();
            BleLog.d(TAG, "检查服务UUID: " + uuid);
            
            // 检查第一阶段OTA服务（APP模式）
            if (APP_OTA_SERVICE_UID.equals(uuid)) {
                BleLog.d(TAG, "✓ 发现一阶段OTA服务 (V1)");
                return new OtaDeviceCheckResult(true, OtaDeviceMode.APP_V1, "发现一阶段OTA服务(V1)");
            }
            
            if (APP_OTA_SERVICE_UID_V2.equals(uuid)) {
                BleLog.d(TAG, "✓ 发现一阶段OTA服务 (V2)");
                return new OtaDeviceCheckResult(true, OtaDeviceMode.APP_V2, "发现一阶段OTA服务(V2)");
            }
            
            // 检查第二阶段OTA服务（Boot模式）
            if (BOOT_OTA_SERVICE_UID.equals(uuid)) {
                BleLog.d(TAG, "✓ 发现二阶段OTA服务 (Boot模式)");
                return new OtaDeviceCheckResult(true, OtaDeviceMode.BOOT, "发现二阶段OTA服务(Boot模式)");
            }
        }
        
        BleLog.d(TAG, "✗ 未发现任何OTA服务");
        return new OtaDeviceCheckResult(false, OtaDeviceMode.NON_OTA, "未发现任何OTA服务");
    }
    
    /**
     * OTA设备检查结果
     */
    public static class OtaDeviceCheckResult {
        public final boolean isOtaDevice;
        public final OtaDeviceMode deviceMode;
        public final String message;
        
        public OtaDeviceCheckResult(boolean isOtaDevice, OtaDeviceMode deviceMode, String message) {
            this.isOtaDevice = isOtaDevice;
            this.deviceMode = deviceMode;
            this.message = message;
        }
        
        @Override
        public String toString() {
            return "OtaDeviceCheckResult{" +
                   "isOtaDevice=" + isOtaDevice +
                   ", deviceMode=" + deviceMode +
                   ", message='" + message + '\'' +
                   '}';
        }
    }
    
    /**
     * OTA设备模式枚举
     */
    public enum OtaDeviceMode {
        APP_V1("APP模式 (V1协议)"),
        APP_V2("APP模式 (V2协议)"),
        BOOT("Boot模式"),
        NON_OTA("非OTA设备"),
        UNKNOWN("未知模式");
        
        private final String description;
        
        OtaDeviceMode(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
} 